#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>

using std::cout;
using std::endl;
using std::cin;
using std::istream;
using std::ostream;
using std::vector;
using std::string;


// 定义类Person，包含姓名（char*）、年龄，提供成员函数 display输出信息。
// 类Employee由Person派生，包含部门（char*）、工资, 
// 成员函数display用来输出职工姓名、年龄、部门、工资。
// 要求Employee对象能够进行正确的复制、赋值操作，同时提供计算平均工资的功能。 

class Person
{
public:
    Person(const char* name, int age);
    Person(const Person& rhs);
    ~Person();
    Person& operator=(const Person& rhs);
    void display(void) const;
private:
    char* _name;
    int _age;
};

Person::Person(const char *name, int age)
    : _name(new char[strlen(name) + 1] ())
    , _age(age){
    strcpy(_name, name);
}

Person::Person(const Person& rhs)
    : _name(new char[strlen(rhs._name + 1)] ())
    , _age(rhs._age){
        strcpy(_name, rhs._name);
}

Person::~Person(){
    if(_name){
        delete [] _name;
        _name = nullptr;
    }
}

Person& Person::operator=(const Person& rhs){
    delete [] _name;
    _name = new char[strlen(rhs._name) + 1] ();
    strcpy(_name, rhs._name);
    return *this;
}

void Person::display(void) const{
    cout << "name: " << _name;
    cout << " age: " << _age;
    // cout << endl;
}


class Employee
    : public Person
{
public:
    Employee(const char* name, int age, const char *department, double salary);
    Employee(const Employee& rhs);
    ~Employee();
    Employee& operator=(const Employee& rhs);
    void display(void) const;
    static void add(double salary);
    static double averageSalary(void);
private:
    char* _department;
    double _salary;
    static double _totle;
    static int _persons;

};
double Employee::_totle = 0;
int Employee::_persons = 0;


Employee::Employee(const char* name, int age, const char *department, double salary)
    : Person(name, age)
    , _department(new char[strlen(department) + 1] ())
    , _salary(salary){
    add(salary);
    strcpy(_department, department);
}

Employee::Employee(const Employee &rhs)
    : Person(rhs)
    , _department(new char[strlen(rhs._department) + 1] ())
    , _salary(rhs._salary){
    add(rhs._salary);
    strcpy(_department, rhs._department);
}

Employee::~Employee(){
    if(_department){
        delete [] _department;
        _department = nullptr;
    }
}

Employee& Employee::operator=(const Employee &rhs){
    Person::operator=(rhs);
    delete [] _department;
    _department = new char[strlen(rhs._department) + 1] ();
    strcpy(_department, rhs._department);
    _totle -= _salary;
    _totle += rhs._salary;
    return *this;
}

void Employee::display(void) const{
    Person::display();
    cout << " department: " << _department;
    cout << " salary: " << _salary;
    cout << endl;
}

void Employee::add(double salary){
    _totle += salary;
    ++_persons;
}

double Employee::averageSalary(void){
    return (_totle / _persons);
}


int main(void)
{
#if 0
    Employee E1("斛珠", 18, "外交部", 1000000);
    E1.display();
    Employee E2 = E1;
    E1.display();

    Employee E3("张三", 19, "司法部", 999999);
    E2 = E3;
    E2.display();
    E3.display();
#endif

    Employee E1("1", 18, "1", 1234);
    Employee E2("2", 18, "1", 5678);
    Employee E3("3", 18, "1", 9101);
    Employee E4("4", 18, "1", 1121);
    
    cout << Employee::averageSalary() << endl;

    return 0;
}




